<?php
/**
 * ads site控制器
 *
 * @package     Include  (controller)
 * @author      hary <hary@taomee.com>
 * @version     $ID 2010-3-31 $
 */
class Admin_modal extends Controller
{
	private $result;

	/**
	 * 构造函数
	 */
	function __construct()
	{
		parent::__construct();

		$this->loadModel('ads_modal_model');

		$this->loadClass('Mcache');

		$this->tpl->template_dir = SYS_PATH  . 'template/admin/modal/';
	}

	/**
	 * 默认执行方法
	 *
	 * @return
	 */
	function index()
	{
		$this->getModals();
	}

	/**
	 * 添加Modal
	 * @return max
	 */
	function addModal(){
		$this->tpl->assign("result",$this->result);
		$this->tpl->display("modal.html");
	}

	/**
	 * 编辑Modal
	 * @return max
	 */
	function EditModal(){

		$id = intval($this->in_var['item']);

		$Modal = $this->ads_modal_model->getModalById($id);

		$this->tpl->assign("modal",$Modal);
		$this->tpl->display("modal.html");
	}

	/**
	 * 获取Modal信息列表
	 * @return max
	 */
	function getModals(){

		$page = max($this->in_var['page'],1);
		$this->result['modals'] = $this->ads_modal_model->getModalsByKeywords('',$page,5);

		$page_info = $this->ads_modal_model->getPageInfo('/admin'.get_url(strtolower(__CLASS__), __FUNCTION__, 'page={Page}'), 3);

		$this->tpl->assign("page_info",$page_info);
		$this->tpl->assign("result",$this->result);
		$this->tpl->display("modals.html");
	}

	/**
	 * 根据条件查询Modal
	 * @return max
	 */
	function searchModals(){

		$page     = max($this->in_var['page'],1);
		$keywords = $this->in_var['keywords'];

		$this->result['modals'] = $this->ads_modal_model->getModalsByKeywords($keywords,$page,5);
		$page_info = $this->ads_modal_model->getPageInfo('/admin'.get_url(strtolower(__CLASS__), __FUNCTION__, 'page={Page}'), 3);

		$this->tpl->assign("page_info",$page_info);
		$this->tpl->assign("result",$this->result);
		$this->tpl->display("modals.html");

	}

	/**
	 * 删除Modal
	 * @return max
	 */
	function delModals(){

		$ids = $this->in_var['item'];
		$id_ = explode(',',$ids);

		try{
			foreach($id_ as $id)
			{
				$this->ads_modal_model->delModalById($id);
			}
		}
		catch(Exception $e)
		{
			exit(json_encode( array('info'=>$this->sys_conf_var['ads']['customer_msg']['modal']['del_error'],'status'=>false)));
		}

		exit(json_encode(array('info'=>$this->sys_conf_var['ads']['customer_msg']['modal']['del_success'],'status'=>true)));
	}

	/**
	 * 保存Modal
	 * @return max
	 */
	function saveModal(){

		$flag  = false;
		$Modal = $this->in_var['modal'];

		if(empty($Modal['mt_name'])||empty($Modal['mt_formwork_name']))
		{
			exit(json_encode(array('info'=>$this->sys_conf_var['ads']['customer_msg']['modal']['error'],'status'=>false)));
		}

		if(!empty($Modal['mt_id']))
		{
			$flag = $this->ads_modal_model->changeModal($Modal);
		}
		else
		{
			$flag = $this->ads_modal_model->addModal($Modal);
		}
		
		exit(json_encode($flag?array('info'=>$this->sys_conf_var['ads']['customer_msg']['modal']['save_success'],'status'=>true):array('info'=>$this->sys_conf_var['ads']['customer_msg']['modal']['save_error'],'status'=>false)));
	}

}